﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class FinanceSalesDetailRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15321;
        protected GridView gvFinanceSalesDetailReport;
        protected GridView gvFinanceSalesDetailReportShowDate;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnShowLot;
        protected Label lblFinanceSalesDetailReportList;
        protected Label lblMsg;
        protected Label lblTotalExtendedPrice;
        protected Label lblTotalExtendedPriceText;
        protected Label lblTotalQty;
        protected Label lblTotalQtyText;
        protected Label lblTotalUnitPrice;
        protected Label lblTotalUnitPriceText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalExtendedPrice;
        private decimal TotalQty;
        private decimal TotalUnitPrice;
        protected UpdatePanel up1;

        public void BindData2gvDiffCountReport(FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry)
        {
            this.PagerFooter.CurrentPage = int.Parse(financeSalesDetailRptQueryEntiry.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetFinanceSalesDetailReportDataSet(financeSalesDetailRptQueryEntiry);
            if (financeSalesDetailRptQueryEntiry.SHOW_DATE == "N")
            {
                base.BindData2GridView(this.gvFinanceSalesDetailReport, ds);
            }
            else
            {
                base.BindData2GridView(this.gvFinanceSalesDetailReportShowDate, ds);
            }
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
            financeSalesDetailRptQueryEntiry.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = financeSalesDetailRptQueryEntiry.CurrentPage;
            financeSalesDetailRptQueryEntiry.CurrentPage = "0";
            this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
            financeSalesDetailRptQueryEntiry.IsGetAll = false;
            financeSalesDetailRptQueryEntiry.CurrentPage = currentPageIndex;
            this.SetFinanceSalesDetailRptQueryEntiry(financeSalesDetailRptQueryEntiry);
            if (financeSalesDetailRptQueryEntiry.SHOW_DATE == "N")
            {
                this.gvFinanceSalesDetailReport.AllowSorting = false;
                this.gvFinanceSalesDetailReport.ShowFooter = false;
                base.Export1Excel("FinanceSalesDetailRpt.xls", this.gvFinanceSalesDetailReport);
            }
            else
            {
                this.gvFinanceSalesDetailReportShowDate.AllowSorting = false;
                this.gvFinanceSalesDetailReportShowDate.ShowFooter = false;
                base.Export1Excel("FinanceSalesDetailRpt.xls", this.gvFinanceSalesDetailReportShowDate);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
            financeSalesDetailRptQueryEntiry.CurrentPage = "0";
            this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
            financeSalesDetailRptQueryEntiry.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
            financeSalesDetailRptQueryEntiry.OwnerID = "";
            financeSalesDetailRptQueryEntiry.CustomerID = "";
            financeSalesDetailRptQueryEntiry.SkuID = "";
            financeSalesDetailRptQueryEntiry.StartDatetime = "";
            financeSalesDetailRptQueryEntiry.EndDatetime = "";
            financeSalesDetailRptQueryEntiry.SHOW_DATE = "N";
            this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
            if (int.Parse(financeSalesDetailRptQueryEntiry.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                financeSalesDetailRptQueryEntiry.CurrentPage = (int.Parse(financeSalesDetailRptQueryEntiry.CurrentPage) + 1).ToString();
                this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
            if (int.Parse(financeSalesDetailRptQueryEntiry.CurrentPage) > 0)
            {
                financeSalesDetailRptQueryEntiry.CurrentPage = (int.Parse(financeSalesDetailRptQueryEntiry.CurrentPage) - 1).ToString();
                this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
            this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
        }

        private FinanceSalesDetailRptQueryEntiry GetFinanceSalesDetailRptQueryEntiry()
        {
            return (base.PageQueryEntitySession as FinanceSalesDetailRptQueryEntiry);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
                financeSalesDetailRptQueryEntiry.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
            }
        }

        protected void gvFinanceSalesDetailReport_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15321", this.gvFinanceSalesDetailReport);
        }

        protected void gvFinanceSalesDetailReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (((Label) e.Row.FindControl("lblQTY")) != null)
                {
                    this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblQTY")).Text != "") ? ((Label) e.Row.FindControl("lblQTY")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblUNIT_PRICE")) != null)
                {
                    this.TotalUnitPrice += decimal.Parse((((Label) e.Row.FindControl("lblUNIT_PRICE")).Text != "") ? ((Label) e.Row.FindControl("lblUNIT_PRICE")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblEXTENDED_PRICE")) != null)
                {
                    this.TotalExtendedPrice += decimal.Parse((((Label) e.Row.FindControl("lblEXTENDED_PRICE")).Text != "") ? ((Label) e.Row.FindControl("lblEXTENDED_PRICE")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalQty);
                this.lblTotalUnitPriceText.Text = base.GetPreferenceDigit(this.TotalUnitPrice);
                this.lblTotalExtendedPriceText.Text = base.GetPreferenceDigit(this.TotalExtendedPrice);
            }
        }

        protected void gvFinanceSalesDetailReport_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
                financeSalesDetailRptQueryEntiry.SortField = e.SortExpression;
                if (financeSalesDetailRptQueryEntiry.SortDirection == "ASC")
                {
                    financeSalesDetailRptQueryEntiry.SortDirection = "DESC";
                }
                else if (financeSalesDetailRptQueryEntiry.SortDirection == "DESC")
                {
                    financeSalesDetailRptQueryEntiry.SortDirection = "ASC";
                }
                this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void gvFinanceSalesDetailReportShowDate_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15321", this.gvFinanceSalesDetailReportShowDate);
        }

        private void LoadData()
        {
            FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = new FinanceSalesDetailRptQueryEntiry();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    financeSalesDetailRptQueryEntiry.OwnerID = this.PreviousPage.OwnerID;
                    financeSalesDetailRptQueryEntiry.CustomerID = this.PreviousPage.CustomerID;
                    financeSalesDetailRptQueryEntiry.SkuID = this.PreviousPage.SkuID;
                    financeSalesDetailRptQueryEntiry.StartDatetime = this.PreviousPage.StartDatetime;
                    financeSalesDetailRptQueryEntiry.EndDatetime = this.PreviousPage.EndDatetime;
                    financeSalesDetailRptQueryEntiry.SHOW_DATE = this.PreviousPage.ShowDate;
                }
                else
                {
                    financeSalesDetailRptQueryEntiry.OwnerID = "";
                    financeSalesDetailRptQueryEntiry.CustomerID = "";
                    financeSalesDetailRptQueryEntiry.SkuID = "";
                    financeSalesDetailRptQueryEntiry.StartDatetime = "";
                    financeSalesDetailRptQueryEntiry.EndDatetime = "";
                    financeSalesDetailRptQueryEntiry.SHOW_DATE = "N";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            financeSalesDetailRptQueryEntiry.IsGetAll = false;
            financeSalesDetailRptQueryEntiry.CurrentPage = "0";
            financeSalesDetailRptQueryEntiry.SortField = "OD.SKU_ID";
            financeSalesDetailRptQueryEntiry.SortDirection = "DESC";
            if (financeSalesDetailRptQueryEntiry.SHOW_DATE == "N")
            {
                financeSalesDetailRptQueryEntiry.PageSize = this.gvFinanceSalesDetailReport.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvFinanceSalesDetailReport.PageSize;
            }
            else
            {
                financeSalesDetailRptQueryEntiry.PageSize = this.gvFinanceSalesDetailReportShowDate.PageSize.ToString();
                this.PagerFooter.PageSize = this.gvFinanceSalesDetailReportShowDate.PageSize;
            }
            this.SetFinanceSalesDetailRptQueryEntiry(financeSalesDetailRptQueryEntiry);
            this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvFinanceSalesDetailReport);
                base.InitAnalyticsReportGridview(this.gvFinanceSalesDetailReportShowDate);
                this.LoadData();
            }
        }

        public void SetFinanceSalesDetailRptQueryEntiry(FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry)
        {
            base.PageQueryEntitySession = financeSalesDetailRptQueryEntiry;
        }

        public FinanceSalesDetailRptQuery PreviousPage
        {
            get
            {
                return (FinanceSalesDetailRptQuery) base.PreviousPage;
            }
        }
    }
}

